from __future__ import division

inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/queryTermPairFreqFor100KQueries"
inputFileHandler = open(inputFileName,"r")
totalFreq = 0
for line in inputFileHandler.readlines():
    lineElements = line.strip().split(" ") 
    totalFreq += int(lineElements[1])    
print "totalFreq:",totalFreq
inputFileHandler.close()

inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/queryTermPairFreqFor100KQueries"
inputFileHandler = open(inputFileName,"r")

outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/queryTermPairNoDirectionProbabilityFor100KQueries"
outputFileHandler = open(outputFileName,"w")

outputFileHandler.write("term_pair has_direction_freq no_direction_probability" + "\n")

for line in inputFileHandler.readlines():
    lineElements = line.strip().split(" ") 
    currentTermPairFreq = int(lineElements[1])    
    currentTermPairNoDirectionProbability = currentTermPairFreq / totalFreq * 2
    outputFileHandler.write(line.strip() + " " + str( currentTermPairNoDirectionProbability ) + "\n")

outputFileHandler.close()
inputFileHandler.close()

'''
# The test has been passed.
# the following is just for checking
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/queryTermPairNoDirectionProbabilityFor100KQueries"
inputFileHandler = open(inputFileName,"r")
totalFreq = 0
totalProb = 0.0
for line in inputFileHandler.readlines():
    lineElements = line.strip().split(" ")
    totalFreq += int(lineElements[1])
    totalProb += float(lineElements[2])

print "totalFreq:",totalFreq
print "totalProb:",totalProb
inputFileHandler.close()
'''








